<?php
	class Matches {

		function __construct(){
			FunctionsHelper::includeFiles(
				array(
					ADMINISTRATOR_MODEL_DIRECTORY . 'model.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'match.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'international_match.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'player.php',
					ADMINISTRATOR_CONTROLLER_DIRECTORY . 'players.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'round.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'tournament.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'country.php'
				)
			);
		}

		private function _getNormalMatch($tournament, $match, $round, $id){
			$arrMatch = array (
				'match_id' => $id,
				'player1' => $match[0],
				'player2' => $match[1],
				'tournament' => $tournament,
				'result' => $match[2],
				'round' => $round
			);
			return new Match($arrMatch);
		}
		
		private function _getInternationalMatch($tournament, $match, $round, $place, $surface, $id){
			$arrMatch = array (
				'match_id' => $id,
				'country1' => $match[0],
				'country2' => $match[1],
				'tournament' => $tournament,
				'result' => $match[2],
				'round' => $round, 
				'place' => $place,
				'surface' => $surface
			);
			return new InternationalMatch($arrMatch);
		}
		
		
		public function insertMatches($arrInitialValues = null){
			$normal_tournament = $arrInitialValues['normal_tournament']; 
			unset($arrInitialValues['normal_tournament']);
			$tournament = $arrInitialValues['tournament_id']; 
			unset($arrInitialValues['tournament_id']);
		

			foreach ($arrInitialValues as $key => $match){
				if (!strstr($key, 'place')){
					$data = explode('_', $key);
					$id = strstr($key, 'mod') ? $data[2] : null;
					
					if ($normal_tournament == 1){
						$match = $this->_getNormalMatch($tournament, $match, $data[1], $id);				
					}else{
						$placeResult = $arrInitialValues['place_' . $key][0];
						if (!strstr($placeResult, '|')) $placeResult .= '| ';
						
						$placeResult = explode('|', $placeResult);
						$place = $placeResult[0];
						$surface = $placeResult[1];

						$match = $this->_getInternationalMatch($tournament, $match, $data[1], $place, $surface, $id);
					}
					
					if ($match->getMatchId() == 0)
						$match->insert();
					else
						$match->update();
				}
			}
			
		}
		
		public function getAllFromSerie($id){
			$match = new InternationalMatch(array('match_id' => $id));
			$arrMatches = $match->getAllMyMatches();

			foreach ($arrMatches as &$match){
				if ($match['player1'] != 0)
					$match['player1'] = $this->getPlayerName($match['player1']);
				if ($match['player2'])
					$match['player2'] = $this->getPlayerName($match['player2']);
				if ($match['player3'])
					$match['player3'] = $this->getPlayerName($match['player3']);
				if ($match['player4'])
					$match['player4'] = $this->getPlayerName($match['player4']);
			}
			return json_encode($arrMatches);
		}
		
		private function getPlayerName($playerId){
			$players = new Players();
			$player = $players->getPlayer(
				array(
					'player_id' => $playerId
				)
			);
			return $player->getName() . ' ' . $player->getSurname();
		}
		
		public function insertInternationalMatch($arrInitialValues = null){
			$internationalMatch = new InternationalMatch(array('match_id' => $arrInitialValues['serie_id']));	
		
			$internationalMatch = new Match();
			$arrHeader = array (
				'tournament_id' => $arrInitialValues['tournament_id_popup'],
				'serie_id' => $arrInitialValues['serie_id'],
			);

			$matches = $arrInitialValues['matches'];
			$arrMatch = array();

			foreach ($matches as $key => $match){
				$arrPlayers = array();
				$t = 0;
				foreach ($match['players'] as $index => $id){
					if ($id != "null"){
						$arrPlayers[] = $id;
						$t++;
					}else{
						$arrPlayers[] = 0;
					}
				}

				if ($t == 0) continue;
				
				$arrMatch['type'] = $t == 2 ? 'S' : 'D';
				$arrMatch['header'] = $arrHeader;
				$arrMatch['result'] = $match['result'];
				$arrMatch['players'] = $arrPlayers;
				
				if (!isset($match['id']) || $match['id'] == "null"){
					$internationalMatch->insertMatch($arrMatch);
				}else{
					$arrMatch['id'] = $match['id'];
					$internationalMatch->updateMatch($arrMatch);
				}
			}
			return 1;
		}
		
		private function _cleanValues($arrInitialValues){
			unset($arrInitialValues['country1']);
			unset($arrInitialValues['country2']);
			unset($arrInitialValues['controller']);
			unset($arrInitialValues['operation']);
			unset($arrInitialValues['serie_id']);
			unset($arrInitialValues['tournament_id']);
			return $arrInitialValues;
		}
	}
?>